package Gr

import (
	"database/sql"
	"fmt"
	"db2s/ETL"
	"db2s/global"
	"db2s/ref"
	"strings"
)

func (vm *ViewMe) Set(parameter string, value any) (err error) {
	return ref.UpdateStructJsonValue(vm, parameter, value)
}

func (vm ViewMe) Result(object string) global.Return {
	switch object {
	default:
		return global.Return{}
	}
}
func (vm *MViewMe) Set(parameter string, value any) (err error) {
	return ref.UpdateStructJsonValue(vm, parameter, value)
}

func (vm MViewMe) Result(object string) global.Return {
	switch object {
	default:
		return global.Return{}
	}
}
func (vm ViewMe) Name(Con *sql.DB) (result global.Return, err error) {
	var event = "[Name]"
	var execSql []string
	vm.Parameter.Con = Con
	execSql = append(execSql, fmt.Sprintf("select "+
		"TABLE_NAME from "+
		"information_schema.views "+
		"where TABLE_SCHEMA ='%v' %v", vm.Parameter.Object.Schema, func() string {
		switch {
		case vm.Parameter.Object.Table == "*":
			return ""
		case strings.Contains(vm.Parameter.Object.Table, "%"):
			return fmt.Sprintf("and TABLE_NAME like '%v'", vm.Parameter.Object.Table)
		default:
			return fmt.Sprintf("and TABLE_NAME = '%v'", vm.Parameter.Object.Table)
		}
	}()))
	if result, err = forExecQuerySql(event, vm.Parameter, ETL.StringGarth, execSql); err != nil || result.Result == nil {
		return
	}
	return
}
func (vm MViewMe) Name(_ *sql.DB) (result global.Return, err error) {
	return
}
func (vm ViewMe) Existence(Con *sql.DB) (result global.Return, err error) {
	var event = "[Existence]"
	vm.Parameter.Con = Con
	var execSql []string
	execSql = append(execSql, fmt.Sprintf("TABLE_NAME from information_schema.views where  TABLE_SCHEMA ='%v'  and TABLE_NAME = '%v'", vm.Parameter.Object.Schema, vm.Parameter.Object.Table))
	if result, err = forExecQuerySql(event, vm.Parameter, ETL.String, execSql); err != nil || result.Result == nil {
		return
	}
	switch {
	case strings.EqualFold(fmt.Sprintf("%v", result.Result), vm.Parameter.Object.Table):
		result.Result = true
	default:
		result.Result = false
	}
	return
}
func (vm MViewMe) Existence(_ *sql.DB) (result global.Return, err error) {
	return
}
func (vm ViewMe) ViewDrop(Con *sql.DB) (result global.Return, err error) {
	//var event = "[ViewDrop]"
	//vm.Parameter.Con = Con
	//result.Sql = fmt.Sprintf("DROP VIEW IF EXISTS %v.%v", vm.Parameter.Object.Schema, vm.Parameter.Object.Table)
	//if result.TimeOut, result.Result, err = newExecUpdateSql(ExecSqlInputS{
	//	Ss:           vm.Parameter,
	//	Event:        event,
	//	UpdateSqlStr: []string{result.Sql},
	//}); err != nil {
	//	return
	//}
	return
}
func (vm MViewMe) ViewDrop(Con *sql.DB) (result global.Return, err error) {
	return
}

func (vm ViewMe) ViewDefinition(Con *sql.DB) (result global.Return, err error) {
	return
}
func (vm MViewMe) ViewDefinition(Con *sql.DB) (result global.Return, err error) {
	return
}
